<include file="Index/header" />
<body>
<div class="panel admin-panel">
  <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>{$title}</strong></div>
  <div class="body-content">
    <form class="form-x"  enctype="multipart/form-data" name="user">
      <div class="form-group">
        <div class="label">
          <label>角色名称：</label>
        </div>
        <div class="field">
            <input type="hidden" value="{$roleId}" name="role_id">
          <input type="text" class="input w50" value="{$role_name}" name="role_name" title="{$roleId}"  placeholder="请输入角色名称"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="role_name" style="color:#F00;"></span>
          <div class="tips"></div>
        </div>
      </div>
        <div class="form-group">
            <div class="label">
                <label>别名：</label>
            </div>
            <div class="field">
                <input type="text" class="input w50" value="{$eName}" name="eName"  placeholder="请输入别名"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="eName" style="color:#F00;"></span>
                <div class="tips"></div>
            </div>
        </div>
      <div class="clear"></div>
      <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
          <div class="button bg-main icon-check-square-o tijiao" style="cursor:pointer;"> 提交</div>
        </div>
      </div>
    </form>
  </div>
</div>

</body></html>
<script>
    //验证角色名称唯一性
    function checkRole(name,obj,id){
        $.ajax({
            url:'/Admin/Role/checkRoleUnique',
            type:'post',
            data:{role_name:name,id:id},
            dataType:'json',
            success:function(re){
                obj.html(re.tips);
            }
        });
    }
    //验证角色名称
    var attr_name=obj_tips='';
    $('input[name="role_name"]').blur(function(){
        var id=$(this).prop("title");
        attr_name=$(this).prop('name');
        obj_tips=$('#'+attr_name);
        if($(this).val()==''){
            obj_tips.html('用户名不能为空！');
        }else{
            checkRole($(this).val(),obj_tips,id);
        }
    });
    //验证别名
    var attr_name=obj_tips='';
    $('input[name="eName"]').blur(function(){
        attr_name=$(this).prop('name');
        obj_tips=$('#'+attr_name);
        if($(this).val()==''){
            obj_tips.html('别名不能为空！');
        }
    });

    var role_name=eName='';
    function checkForm(){
        role_name=$('input[name="role_name"]').val();
        if(role_name==''){
            $('#role_name').html('角色名称不能为空！');
            return false;
        }
        eName=$('input[name="eName"]').val();
        if(eName==''){
            $('#eName').html('别名不能为空！');
            return false;
        }
    }

    function postData(){
        var id= $('input[name="role_name"]').prop("title");
        var data={role_name:role_name,eName:eName,id:id};
        $.ajax({
            url:'/Admin/Role/ajaxCheckData/scene/edit',
            type:'post',
            data:data,
            dataType:'json',
            success:function(re){
                if(re.error==1){
                    $.each(re.errInfo,function(key,val){
                        $('#'+key).html(val);
                    })
                    window.location.href='#mask';
                }else{
                    $('form[name="user"]').prop('action','edit').prop('method','post');
                    $('form[name="user"]').submit();
                }
            }
        });
    }

    $('.tijiao').click(function(){
        var flag;
        flag=checkForm();//提交前的验证
        if(flag===false){
            window.location.href='#mask';
        }else{
            postData();//发送数据进行验证
        }
    });
</script>